#ifndef _FILENODE_H_
#define _FILENODE_H_

#define ITSUBFILE std::map<tstring,FileNode *>::iterator 

class FileNode
{
public:
	tstring		tsFilename;
	UINT64		uFileGuid;
	UINT64		uFileSize;
	FILETIME	ftCreate;
	FILETIME	ftLastModify;
	FILETIME	ftLastAccess;
	DWORD		dwFileAttributes;
	int			iXlock;
	int			iSlock;

	std::map<tstring,FileNode *> mSubFile;

	void MakeDirNode(tstring tsName)
	{
		MakeFileNode(tsName);
		dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
	}
	void MakeFileNode(tstring tsName)
	{
		tsFilename = tsName;
		dwFileAttributes = FILE_ATTRIBUTE_NORMAL;

		SYSTEMTIME sysTime;
		GetSystemTime(&sysTime);
		SystemTimeToFileTime(&sysTime,&ftCreate);
		ftLastAccess = ftLastModify = ftCreate;
		uFileSize = 0;
		iSlock = iXlock = 0;
	}
	int CreateSubDir(UINT64 uGuid,tstring tsName)
	{
		FileNode *pFile = new FileNode;
		if( pFile == NULL )
			return 1;
		pFile->MakeDirNode(tsName);
		pFile->uFileGuid = uGuid;
		pFile->mSubFile[ _TEXT(".") ] = pFile;
		pFile->mSubFile[ _TEXT("..") ] = this;
		mSubFile[YY::LowCase(tsName)] = pFile;
		return 0;
	}
	int CreateSubFile(UINT64 uGuid,tstring tsName,DWORD dwAttr = 0)
	{
		FileNode *pFile = new FileNode;
		if( pFile == NULL )
			return 1;
		pFile->MakeFileNode(tsName);
		pFile->uFileGuid = uGuid;
		if( dwAttr != 0 )
		{
			pFile->dwFileAttributes = dwAttr;
		}
		mSubFile[ YY::LowCase(tsName) ] = pFile;
		return 0;
	}
	FileNode *Find(tstring tsName)
	{
		if( tsName.length() < 1 )
			return NULL;
		ITSUBFILE it;
		it = mSubFile.find(YY::LowCase(tsName));
		if( it == mSubFile.end() )
		{
			return NULL;
		}
		return (FileNode *)(it->second);
	}
	int IsDir()
	{
		if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			return 1;
		return 0;
	}
	int MakeString(TCHAR *szStr)
	{
		_stprintf(szStr,_TEXT("%s|%I64u|%I64u|%u|%u|%u|%u|%u|%u|%u"),
			tsFilename.c_str(),
			uFileGuid,
			uFileSize,
			ftCreate.dwLowDateTime,ftCreate.dwHighDateTime,
			ftLastAccess.dwLowDateTime,ftLastAccess.dwHighDateTime,
			ftLastModify.dwLowDateTime,ftLastModify.dwHighDateTime,
			dwFileAttributes);
		int i;
		for(i=0;szStr[i];i++)
		{
			if( szStr[i] == ' ' )
				szStr[i] = '*';
		}
		return 0;
	}
	int ReadString(TCHAR *szStr)
	{
		TCHAR szName[256];
		int i;
		for(i=0;szStr[i];i++)
		{
			if( szStr[i] == (TCHAR)'|' )
				szStr[i] = ' ';
		}
		_stscanf(szStr,_TEXT("%s%I64u%I64u%u%u%u%u%u%u%u"),
			szName,
			&uFileGuid,
			&uFileSize,
			&ftCreate.dwLowDateTime,&ftCreate.dwHighDateTime,
			&ftLastAccess.dwLowDateTime,&ftLastAccess.dwHighDateTime,
			&ftLastModify.dwLowDateTime,&ftLastModify.dwHighDateTime,
			&dwFileAttributes);

		for(i=0;szName[i];i++)
		{
			if( szName[i] == '*' )
				szName[i] = ' ';
		}
		tsFilename = szName;
		iXlock = iSlock = 0;
		return 0;
	}
};


#endif